﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenMediaOrganizer.MediaLibrary;

namespace OpenMediaOrganizer.MediaRenamer
{
    public partial class RenamingCenterForm : Form
    {
        public RenamingCenterForm()
        {
            InitializeComponent();
        }

        OMOLibrary library = new OMOLibrary();

        private void RenamingCenterForm_Load(object sender, EventArgs e)
        {
            RefreshFolders();
        }

        private void RefreshFolders()
        {
            MovieFilesListView.Items.Clear();
            MediaFoldersListView.Items.Clear();
            MovieFilesListView.Items.Clear();
            library.RefreshDirectories();
            foreach (DirectoryInfo dir in library.Directories)
            {
                ListViewItem dirItem = new ListViewItem() { Tag = dir, Text = dir.Name };
                dirItem.SubItems.Add(library.GetNewName(dir));
                MovieFilesListView.Items.Add(dirItem);
            }
            foreach (FileInfo file in library.Files)
            {
                ListViewItem fileItem = new ListViewItem() { Tag = file, Text = file.Name };
                fileItem.SubItems.Add(library.GetNewName(file));
                MovieFilesListView.Items.Add(fileItem);
            }
        }

        private void addRegexToolStripButton_Click(object sender, EventArgs e)
        {
            RegexListView.Items.Add(new ListViewItem(RegexToolStripTextBox.Text));
        }

        private void addRemovalToolStripButton_Click(object sender, EventArgs e)
        {
            RemoveListView.Items.Add(new ListViewItem(RemovalsToolStripTextBox.Text));
        }

        private void renameToolStripButton_Click(object sender, EventArgs e)
        {
            RenameFiles();
        }

        private void RenameFiles()
        {
            foreach (ListViewItem item in MovieFilesListView.Items)
            {
                FileInfo file = (FileInfo)item.Tag;
                if (file.Name != item.SubItems[1].Text)
                {
                    file.MoveTo(Path.Combine(file.FullName.Replace(file.Name,""),item.SubItems[1].Text));
                }
            }
        }

        private void renameFolderToolStripButton_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem folderListViewItem in MovieFilesListView.SelectedItems)
            {
                DirectoryInfo dir = (DirectoryInfo)folderListViewItem.Tag;
                if (dir.Name != folderListViewItem.SubItems[1].Text)
                {
                    dir.MoveTo(Path.Combine(dir.FullName.Replace(dir.Name, ""), folderListViewItem.SubItems[1].Text));
                }
            }
        }

        private void refreshToolStripButton_Click(object sender, EventArgs e)
        {
            RefreshFolders();
        }
    }
}
